//子弹生成位置
var bossBPosition = cc.Class({
    name: 'bossBPosition',
    properties: {
        xAxis: {
            default:'',
            tooltip: '初始x轴，相对hero',
        },
        yAxis: {
            default:'',
            tooltip: '初始y轴，相对hero'
        },
        xSpeed: cc.Integer,//x轴速度
    },
});

//不限时长子弹
var bossBulletInfinite = cc.Class({
    name:'bossBulletInfinite',
    properties: {
        name:'',
        freqTime:0,
        initPollCount:0,
        prefab:cc.Prefab,
        position: {
            default: [],
            type: bossBPosition,
            tooltip: '每次多少排子弹',
        }
    }
});

//有限时长子弹组
var bossBulletFiniteG = cc.Class({
    name:'bossBulletFiniteG',
    extends:bossBulletInfinite,
    properties: {
        finiteTime:0,
        orginName:'',
    }
}); 

var config = require("config");

cc.Class({
    extends: cc.Component,

    properties:()=> ({
        bossBulletInfinite: {
            default:null,
            type: bossBulletInfinite,
            tooltip:'无限时长子弹组'
        },
        bossBulletFiniteG: {
            default:[],
            type: bossBulletFiniteG,
            tooltip:'有限时长子弹组'
        },
        bossBulletFiniteG2: {
            default:[],
            type: bossBulletFiniteG,
            tooltip:'有限时长子弹组'
        },
        // boss: cc.Node,
    }),
    // use this for initialization
    onLoad: function () {
        this.eState = D.commonInfo.gameState.none;
        //初始化无限子弹组
        D.common.initObjPool(this,this.bossBulletInfinite);
        //初始化有限子弹组
        D.common.batchInitObjPool(this,this.bossBulletFiniteG);
        //初始化有限子弹组2
        D.common.batchInitObjPool(this,this.bossBulletFiniteG2);
    },
    startAction: function(){
        this.eState = D.commonInfo.gameState.start;
        //生成子弹
        this.getNewbullet(this.bossBulletInfinite);
        // this.bICallback = function(){this.getNewbullet(this.bossBulletInfinite);}.bind(this);
        // this.schedule(this.bICallback, this.bossBulletInfinite.freqTime);

        // for (var bi=0; bi<this.bossBulletFiniteG.length; bi++){
        //     console.log(">>>>>>>bi:",bi);
        //     this.bFCallback = function(e){this.getNewbullet(this.bossBulletFiniteG[e]);}.bind(this,bi);
        //     this.schedule(this.bFCallback, this.bossBulletFiniteG[bi].freqTime,this.bossBulletFiniteG[bi].finiteTime);
        // }

        // for (var bi=0; bi<this.bossBulletFiniteG2.length; bi++){
        //     console.log(">>>>>>>bi:",bi);
        //     this.bFCallback2 = function(e){this.getNewbullet(this.bossBulletFiniteG2[e]);}.bind(this,bi);
        //     this.schedule(this.bFCallback2, this.bossBulletFiniteG2[bi].freqTime,this.bossBulletFiniteG2[bi].finiteTime);
        // }

        // this.autoChangeBullet();
    },
    autoChangeBullet(){
        var self = this;
        this.node.stopAllActions();
        // config.BossBulletChangeCount = 0;
        this.stopAction();
        this.bICallback = function(){this.getNewbullet(this.bossBulletInfinite);}.bind(this);
        this.schedule(this.bICallback, this.bossBulletInfinite.freqTime);

        var delay1 = cc.delayTime(10);
        var delay2 = cc.delayTime(10);
        var callFunc1 = cc.callFunc(function(){
            self.changeBullet("doubleBullet")
        });
        var callFunc2 = cc.callFunc(function(){
            self.changeBullet("threeBullet")
        });
        this.node.runAction(cc.sequence(delay1,callFunc1,delay2,callFunc2));
    },
    stopChangeBullet(){
        this.node.stopAllActions();
    },
    stopAction(){
        // this.unscheduleAllCallbacks();
        this.unschedule(this.bICallback);
        this.unschedule(this.bFCallback);
        this.unschedule(this.bFCallback2);
    },
    pauseAction: function(){
        this.enabled = false;
        this.eState = D.commonInfo.gameState.pause;
        console.log("this.eState:",this.eState);
        this.node.pauseAllActions();
    },
    resumeAction: function(){
        this.enabled = true;
        this.eState = D.commonInfo.gameState.start;
        this.node.resumeAllActions();
    },
    //换子弹
    changeBullet: function(ufoBulletName){
        this.unschedule(this.bICallback);
        this.unschedule(this.bFCallback);
        this.unschedule(this.bFCallback2);
        for (var bi=0; bi<this.bossBulletFiniteG.length; bi++){ 
            if (this.bossBulletFiniteG[bi].orginName == ufoBulletName){
                this.bFCallback = function(e){this.getNewbullet(this.bossBulletFiniteG[e]);}.bind(this,bi);
                this.schedule(this.bFCallback, this.bossBulletFiniteG[bi].freqTime,this.bossBulletFiniteG[bi].finiteTime);
                
                // var delay = this.bossBulletFiniteG[bi].freqTime * this.bossBulletFiniteG[bi].finiteTime;
                // this.schedule(this.bICallback,this.bossBulletInfinite.freqTime,cc.macro.REPEAT_FOREVER,delay);
            }
        }

        for (var bi=0; bi<this.bossBulletFiniteG2.length; bi++){ 
            if (this.bossBulletFiniteG2[bi].orginName == ufoBulletName){
                this.bFCallback2 = function(e){this.getNewbullet(this.bossBulletFiniteG2[e]);}.bind(this,bi);
                this.schedule(this.bFCallback2, this.bossBulletFiniteG2[bi].freqTime,this.bossBulletFiniteG2[bi].finiteTime);
                
                var delay = this.bossBulletFiniteG2[bi].freqTime * this.bossBulletFiniteG2[bi].finiteTime;
                this.schedule(this.bICallback,this.bossBulletInfinite.freqTime,cc.macro.REPEAT_FOREVER,delay);
            }
        }
    },
    //生成子弹
    getNewbullet: function (bulletInfo) {
        var poolName = bulletInfo.name + 'Pool';
        for (var bc=0; bc<bulletInfo.position.length; bc++){
            // console.log(".......bc:",bulletInfo.position[bc].xSpeed)
            var newNode = D.common.genNewNode(this[poolName],bulletInfo.prefab,this.node);
            var newV2 = this.getBulletPostion(bulletInfo.position[bc]);
            newNode.setPosition(newV2);
            newNode.getComponent('bossBullet').bossBulletGroup = this;
            newNode.getComponent('bossBullet').xSpeed = bulletInfo.position[bc].xSpeed;
        }
    },
    //获取子弹位置
    getBulletPostion: function(posInfo){
        var hPos = cc.v2(0,0);
        var newV2_x = hPos.x + parseInt(posInfo.xAxis);
        var newV2_y = hPos.y + parseInt(posInfo.yAxis);
        return cc.v2(newV2_x, newV2_y); 
    },
    //子弹灭亡
    bulletDied: function(nodeinfo){
        //回收节点
        D.common.backObjPool(this,nodeinfo);
    },
    
});
